package  com.bs.medicine.struts.action;


import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.axis2.AxisFault;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.struts2.ServletActionContext;

import com.bs.entity.MedicineCategory;
import com.bs.entity.MedicineInfo;
import com.bs.medicine.utils.DateHelper;
import com.bs.medicine.utils.DetailLogger;
import com.google.gson.Gson;
import com.mysoap.medicine.service.MedicineInfoSoapStub;
import com.mysoap.medicine.service.MedicineInfoSoapStub.MedicineInfoRequest;
import com.mysoap.medicine.service.MedicineInfoSoapStub.MedicineInfoResponse;
import com.mysoap.medicine.service.MedicineInfoSoapStub.MedicineSaveResponse;
import com.opensymphony.xwork2.ActionSupport;

public class MedicineInfoAction  extends ActionSupport{
	private static MedicineInfoSoapStub medicineInfoSoapStub;
	private static DetailLogger logger = DetailLogger.getDetailLogger(MedicineInfoAction.class);
	
	// 药品信息列表
	private List<MedicineInfo> medicineList = new ArrayList<MedicineInfo>();
	public MedicineInfo medicineInfo;
	// 页面中的关键字
	private String keyWord;
	private List keyWords = new ArrayList();
	private String queryid;
	private MedicineInfo meInfo;

	
	

	public List<MedicineInfo> getMedicineList() {
		return medicineList;
	}
	public void setMedicineList(List<MedicineInfo> medicineList) {
		this.medicineList = medicineList;
	}
	public MedicineInfo getMedicineInfo() {
		return medicineInfo;
	}
	public void setMedicineInfo(MedicineInfo medicineInfo) {
		this.medicineInfo = medicineInfo;
	}
	public String getKeyWord() {
		return keyWord;
	}
	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
	public List getKeyWords() {
		return keyWords;
	}
	public void setKeyWords(List keyWords) {
		this.keyWords = keyWords;
	}
	public String getQueryid() {
		return queryid;
	}
	public void setQueryid(String queryid) {
		this.queryid = queryid;
	}
	public MedicineInfo getMeInfo() {
		return meInfo;
	}
	public void setMeInfo(MedicineInfo meInfo) {
		this.meInfo = meInfo;
	}
	
	static{
		try {
			String address="http://localhost:8080/MySoap/services/MedicineInfoSoap";
			medicineInfoSoapStub = new MedicineInfoSoapStub (address);
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
    /**
     * 新增药品信息
     * @return
     * @throws Exception
     */
	public String addMedicineInfo() throws Exception {
		MedicineInfoSoapStub.MedicineSave medicineSave = new MedicineInfoSoapStub.MedicineSave();
		MedicineInfoRequest request = new MedicineInfoRequest();
		Calendar cal = Calendar.getInstance();	
		com.mysoap.medicine.service.MedicineInfoSoapStub.MedicineInfo param1 = new com.mysoap.medicine.service.MedicineInfoSoapStub.MedicineInfo();
		param1.setMedicineDesc(medicineInfo.getMedicineDesc());
		param1.setMedicineFactory(medicineInfo.getMedicineFactory());
		param1.setMedicineFactoryAddress(medicineInfo.getMedicineFactoryAddress());
		param1.setMedicineName(medicineInfo.getMedicineName());
		param1.setMedicinePrice(medicineInfo.getMedicinePrice());
		HttpServletRequest request1 =ServletActionContext.getRequest();
		String medicineCo =request1.getParameter("medicineCo");
		//String id =request1.getParameter("categoryid");
		Long id = medicineInfo.getMedicineCategory().getCategoryId();
		System.out.println("------------------------id"+id);
		System.out.println("medicineCo"+medicineCo);
		param1.setMedicineCode(medicineCo);
		System.out.println(param1.getMedicineCode());
		cal.setTime(medicineInfo.getBuyTime());
		param1.setBuyTime(cal);
		request.setMedicineInfo(param1);
		request.setMeidcineCode(medicineCo);
		request.setKeyWord("2");//MedicineCategory
		request.setUpdateFlag(1);//MedicineStorage
		medicineSave.setMedicineRequest(request);
		logger.info("添加用户信息请求："+ToStringBuilder.reflectionToString(request,ToStringStyle.MULTI_LINE_STYLE));
		MedicineSaveResponse response = medicineInfoSoapStub.medicineSave(medicineSave);
		logger.info("添加用户信息响应："+ToStringBuilder.reflectionToString(response.get_return(),ToStringStyle.MULTI_LINE_STYLE));
		
		return SUCCESS;
	}
   /**
    * 获取所有药品列表
    * @return
    * @throws Exception
    */
	public String getAllMedicineList() throws Exception {
		MedicineInfoSoapStub.MedicineQuery medicineQuery = new MedicineInfoSoapStub.MedicineQuery();
		MedicineInfoRequest param = new MedicineInfoRequest();
		param.setUpdateFlag(1);
		medicineQuery.setRequest(param);
		logger.info("用户信息查询请求："+ToStringBuilder.reflectionToString(param,ToStringStyle.MULTI_LINE_STYLE));
		MedicineInfoResponse response =medicineInfoSoapStub.medicineQuery(medicineQuery).get_return();
		logger.info("用户信息查询请求："+ToStringBuilder.reflectionToString(response,ToStringStyle.MULTI_LINE_STYLE));
		medicineList = ToList(response);
		return SUCCESS;
	}
    
	/**
	 * 通过关键字查找药品信息
	 * @return
	 * @throws Exception
	 */
	public String getMedicineByKeyWord() throws Exception {
		MedicineInfoSoapStub.MedicineQuery medicineQuery = new MedicineInfoSoapStub.MedicineQuery();
		MedicineInfoRequest param = new MedicineInfoRequest();
		keyWord = new String(keyWord.getBytes("ISO-8859-1"), "utf-8");
		param.setUpdateFlag(2);
		param.setKeyWord(keyWord);
		medicineQuery.setRequest(param);
		logger.info("药品信息查询请求："+ToStringBuilder.reflectionToString(param,ToStringStyle.MULTI_LINE_STYLE));
		MedicineInfoResponse response =medicineInfoSoapStub.medicineQuery(medicineQuery).get_return();
		logger.info("药品信息查询请求："+ToStringBuilder.reflectionToString(response,ToStringStyle.MULTI_LINE_STYLE));
		medicineList = ToList(response);
		return SUCCESS;
	}
    
	/**
	 * 快捷方式获取关键字
	 * @return
	 * @throws Exception
	 */
	public String searchKey() throws Exception {
		MedicineInfoSoapStub.MedicineQuery medicineQuery = new MedicineInfoSoapStub.MedicineQuery();
		MedicineInfoRequest param = new MedicineInfoRequest();
		keyWord = new String(keyWord.getBytes("ISO-8859-1"), "utf-8");
		param.setUpdateFlag(3);
		param.setKeyWord(keyWord);
		medicineQuery.setRequest(param);
		logger.info("关键字查询请求："+ToStringBuilder.reflectionToString(param,ToStringStyle.MULTI_LINE_STYLE));
		MedicineInfoResponse response1 =medicineInfoSoapStub.medicineQuery(medicineQuery).get_return();
		logger.info("关键字查询请求："+ToStringBuilder.reflectionToString(response1,ToStringStyle.MULTI_LINE_STYLE));
	
		keyWords = ToList(response1);
	//	keyWords = medicineService.getKeyWords(keyWord);
		Gson gson = new Gson();
		String key = gson.toJson(keyWords);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		response.setContentType("application/json ;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println(key);
		out.flush();
		out.close();
		return null;
	}
    
	/**
	 * 查看药品详情
	 * @return
	 * @throws Exception
	 */
	public String queryMedicineDetail() throws Exception {
		MedicineInfoSoapStub.MedicineQuery medicineQuery = new MedicineInfoSoapStub.MedicineQuery();
		MedicineInfoRequest param = new MedicineInfoRequest();
		keyWord = new String(keyWord.getBytes("ISO-8859-1"), "utf-8");
		param.setUpdateFlag(4);
		medicineQuery.setRequest(param);
		logger.info("查看药品请求："+ToStringBuilder.reflectionToString(param,ToStringStyle.MULTI_LINE_STYLE));
		MedicineInfoResponse response1 =medicineInfoSoapStub.medicineQuery(medicineQuery).get_return();
		logger.info("查看药品请求："+ToStringBuilder.reflectionToString(response1,ToStringStyle.MULTI_LINE_STYLE));
		meInfo = ToMedicineInfo(response1);
     //    meInfo =medicineService.getMedicineInfoById(queryid);
		 return SUCCESS;
	}
	
	/**
	 * 编辑药品信息前
	 * @return
	 * @throws Exception
	 */
	public String editMedicine()throws Exception{
		MedicineInfoSoapStub.MedicineQuery medicineQuery = new MedicineInfoSoapStub.MedicineQuery();
		MedicineInfoRequest param = new MedicineInfoRequest();
		keyWord = new String(keyWord.getBytes("ISO-8859-1"), "utf-8");
		param.setUpdateFlag(4);
		param.setMedicineId(Long.parseLong(queryid));
		medicineQuery.setRequest(param);
		logger.info("查看药品请求："+ToStringBuilder.reflectionToString(param,ToStringStyle.MULTI_LINE_STYLE));
		MedicineInfoResponse response1 =medicineInfoSoapStub.medicineQuery(medicineQuery).get_return();
		logger.info("查看药品请求："+ToStringBuilder.reflectionToString(response1,ToStringStyle.MULTI_LINE_STYLE));
		meInfo = ToMedicineInfo(response1);
	//	meInfo=medicineService.getMedicineInfoById(queryid);
		return SUCCESS;
	}
	/**
	 * 编辑药品信息
	 * @return
	 * @throws Exception
	 */
	public String editSaveMedicine() throws Exception{
		HttpServletRequest request =ServletActionContext.getRequest();
		String medicinename =request.getParameter("medicinename");
		String factory =request.getParameter("factory");
		String factoryAdd =request.getParameter("factoryAdd");
		String medicineid =request.getParameter("medicineid");
		String desc =request.getParameter("desc");
		String price =request.getParameter("price");
		MedicineInfoSoapStub.MedicineQuery medicineQuery = new MedicineInfoSoapStub.MedicineQuery();
		MedicineInfoRequest param = new MedicineInfoRequest();
		param.setUpdateFlag(4);
		param.setMedicineId(medicineInfo.getMedicineId());
		medicineQuery.setRequest(param);
		logger.info("用户信息查询请求："+ToStringBuilder.reflectionToString(param,ToStringStyle.MULTI_LINE_STYLE));
		MedicineInfoResponse response =medicineInfoSoapStub.medicineQuery(medicineQuery).get_return();
		logger.info("用户信息查询请求："+ToStringBuilder.reflectionToString(response,ToStringStyle.MULTI_LINE_STYLE));
		if(response.getResultCode()==0){
			param.setUpdateFlag(5);
			param.setMedicineId(medicineInfo.getMedicineId());
			param.setMedicineName(medicinename);
			param.setMedicineFactory(factory);
			param.setMedicineFactoryAddress(factoryAdd);
			param.setMedicineDesc(desc);
			param.setMedicinePrice(Double.parseDouble(price));
			medicineQuery.setRequest(param);
			logger.info("用户信息编辑请求："+ToStringBuilder.reflectionToString(param,ToStringStyle.MULTI_LINE_STYLE));
			MedicineInfoResponse editResponse =medicineInfoSoapStub.medicineQuery(medicineQuery).get_return();
			logger.info("用户信息编辑响应："+ToStringBuilder.reflectionToString(editResponse,ToStringStyle.MULTI_LINE_STYLE));
		}
		return SUCCESS;
	}
	
	/**
	 * 高级查询
	 * @return
	 * @throws Exception
	 */
	public String highQuery() throws Exception{
		System.out.println(meInfo==null);
		System.out.println(meInfo.getMedicineName());
		MedicineInfoSoapStub.MedicineQuery medicineQuery = new MedicineInfoSoapStub.MedicineQuery();
		MedicineInfoRequest param = new MedicineInfoRequest();
		keyWord = new String(keyWord.getBytes("ISO-8859-1"), "utf-8");
		param.setUpdateFlag(7);
		param.setMedicineDesc(meInfo.getMedicineDesc());
		param.setMedicineFactory(meInfo.getMedicineFactory());
		param.setMedicineFactoryAddress(meInfo.getMedicineFactoryAddress());
		param.setMedicineId(meInfo.getMedicineId());
		param.setMedicineName(meInfo.getMedicineName());
		param.setMedicinePrice(meInfo.getMedicinePrice());
		param.setMeidcineCode(meInfo.getMedicineCode());
		medicineQuery.setRequest(param);
		logger.info("查看药品请求："+ToStringBuilder.reflectionToString(param,ToStringStyle.MULTI_LINE_STYLE));
		MedicineInfoResponse response1 =medicineInfoSoapStub.medicineQuery(medicineQuery).get_return();
		logger.info("查看药品请求："+ToStringBuilder.reflectionToString(response1,ToStringStyle.MULTI_LINE_STYLE));
		medicineList = ToList(response1);
	//	medicineList =medicineService.highQuery(meInfo);
		return SUCCESS;
	}
    
	/**
	 * 删除药品信息
	 * @return
	 * @throws Exception
	 */
	public String deleteMedicine() throws Exception{
		MedicineInfoSoapStub.MedicineQuery medicineQuery = new MedicineInfoSoapStub.MedicineQuery();
		MedicineInfoRequest param = new MedicineInfoRequest();
		keyWord = new String(keyWord.getBytes("ISO-8859-1"), "utf-8");
		param.setUpdateFlag(6);
		param.setMedicineId(Long.parseLong(queryid));
		param.setMedicineId(Long.parseLong(queryid));
		medicineQuery.setRequest(param);
		logger.info("删除药品请求："+ToStringBuilder.reflectionToString(param,ToStringStyle.MULTI_LINE_STYLE));
		MedicineInfoResponse response1 =medicineInfoSoapStub.medicineQuery(medicineQuery).get_return();
		logger.info("删除药品请求："+ToStringBuilder.reflectionToString(response1,ToStringStyle.MULTI_LINE_STYLE));
	//	MedicineInfo medicineInfo =medicineService.getMedicineInfoById(queryid);
	//	medicineInfo.setMedicineCategory(null);
	//	medicineService.deleteMedicine(medicineInfo);
		return SUCCESS;
	}
	/**
	 * soap请求包到List
	 * @param response
	 * @return
	 * @throws ParseException
	 */
	public List<MedicineInfo> ToList(MedicineInfoResponse response) throws ParseException{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		com.mysoap.medicine.service.MedicineInfoSoapStub.MedicineInfo[] mis = response.getMedlist();
		String[] str = response.getDateArr().split(",");
		String[] cStr = response.getCategoryDesc().split(",");
		for(int i = 0; i<mis.length;i++){
			MedicineInfo m = new MedicineInfo();
			m.setMedicineDesc(mis[i].getMedicineDesc());
			m.setMedicineFactory(mis[i].getMedicineFactory());
			m.setMedicineFactoryAddress(mis[i].getMedicineFactoryAddress());
			m.setMedicineId(mis[i].getMedicineId());
			m.setMedicineName(mis[i].getMedicineName());
			m.setMedicinePrice(mis[i].getMedicinePrice());
			String ss = mis[i].getMedicineCode();
			m.setMedicineCode(ss);
			m.setBuyTime(df.parse(str[i]));
			if(!cStr[i].equals("")|| !cStr[i].equals(null)){
				MedicineCategory mc = new MedicineCategory();
				mc.setCategoryDesc(cStr[i]);
				m.setMedicineCategory(mc);
			}
			
			medicineList.add(m);
		}
		return medicineList;
		
	}
	/**
	 * soap请求包到object
	 * @param response
	 * @return
	 * @throws ParseException
	 */
	public MedicineInfo ToMedicineInfo(MedicineInfoResponse response) throws ParseException{
		meInfo.setBuyTime(DateHelper.string2Date(response.getBuyTime()));
		meInfo.setMedicineCode(response.getMedicineCode());
		meInfo.setMedicineDesc(response.getCategoryDesc());
		meInfo.setMedicineFactory(response.getMedicineFactory());
		meInfo.setMedicineFactoryAddress(response.getMedicineFactoryAddress());
		meInfo.setMedicineId(response.getCategoryId());
		meInfo.setMedicineName(response.getMedicineName());
		meInfo.setMedicinePrice(response.getMedicinePrice());
		return meInfo;
	}
	

	

}
